const Router = require('koa-router')
const router = new Router()
const mongoose = require('mongoose')

//分页查询
router.post('/querybypage',async(ctx)=>{
    try{
        const test = mongoose.model("blogs")
        const result = await test.find(ctx.request.body)
        //必须要是异步的，要加async和await  exec（）不能用
        ctx.body = {code:200,message:result}    
       }
       catch(error){
           console.log(error);
           
       }
 })
  //获取所有标签
  router.get('/gettags',async(ctx)=>{
    try{
        const tags = mongoose.model("tags")
        const result = await tags.find()
        console.log(result);
        
        ctx.body = {code:200,message:result}    
       }
       catch(error){
           console.log(error);
       }
})

  //加载分类标签
  router.get('/loadtags',async(ctx)=>{
    try{
     const test = mongoose.model("blogs")
     const result = await test.find()
     ctx.body = {code:200,message:result}    
    }
    catch(error){
        console.log(error);
    }
 })

 //加载相册
router.get('/refreshphoto',async(ctx)=>{
    try{
     const test = mongoose.model("images")
     const result = await test.find()
     ctx.body = {code:200,message:result}    
    }
    catch(error){
        console.log(error);
    }
 })

 //将相册图片上传到数据库
 router.post('/uploadtodatabase',async(ctx)=>{
    try{
     const uploadImg = ctx.request.body
     const img = mongoose.model("images")
     const up = new img(uploadImg)
     await up.save()
    }catch(error){
        console.log(error);
    }
 })

//搜索博客
router.post('/search',async(ctx)=>{
    try{
     const test = mongoose.model("blogs")
     const result = await test.find(ctx.request.body)
     //必须要是异步的，要加async和await  exec（）不能用
     ctx.body = {code:200,message:result}    
    }
    catch(error){
        console.log(error);
        
    }
 })
 
module.exports = router